package com.zzy.util;

import org.apache.commons.codec.binary.BinaryCodec;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.servlet.http.HttpServletRequest;
import javax.swing.filechooser.FileSystemView;

public class UtilPath {
    /**
     *
     * 运行时候的项目路径类似：
     *  http://localhost:8099/TZZY/
     */
    public static  String  getrealPath(HttpServletRequest request){
        String paths = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName()+ ":" + request.getServerPort() + paths + "/";
        return basePath;
    }

    /***
     * 获取服务器下项目的路径类似：
     * D:\worksoft\tocatexe\webapps\TZZY\
     * 一般是jsp等外层路径
     * @param request
     * @return
     */
    public static  String  getTomcatPath(HttpServletRequest request){
        String realPath = request.getSession().getServletContext().getRealPath("/");
        return realPath;
    }


    /**
     * 获取桌面路径类似：
     *  C:\Users\zzy\Desktop
     * @param
     */
    public static String getDeskTopPath(){
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        return desktopPath;
    }


    /**
     * 获取classes路径类似：
     * 一般是java代码目录路径
     *  D:/worksoft/tocatexe/webapps/TZZY/WEB-INF/classes/
     *  用服务器项目路径拼接也可以实现，方法[getTomcatPath]
     *
     * @param
     */
    public static String getClassesPath(){
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        return desktopPath;
    }



    public void getPath(HttpServletRequest request) {

        String paths = request.getContextPath();
        String basePath = request.getScheme() + "://" + request.getServerName()
                + ":" + request.getServerPort() + paths + "/";
        System.out.println("第一个路径：" + basePath);

        // D:\worksoft\tocatexe\webapps\TZZY\
        String realPath = request.getSession().getServletContext().getRealPath(
                "/");
        System.out.println("第二个路径：" + realPath);

        // C:\Users\zzy\Desktop
        File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
        String desktopPath = desktopDir.getAbsolutePath();
        System.out.println("桌面路径：" + desktopPath);

        // D:/worksoft/tocatexe/webapps/TZZY/WEB-INF/classes/
        String basePath03 = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        System.out.println("第六个路径：" + basePath03);
    }



    /**
     * 根据 Properties 配置文件的		键	读取	值
     * 项目是启动的
     * @param  Profilename 配置文件的名字
     * @param  proPath 配置文件的地址 	null 和 "" 认为是src 目录下
     * @param  strkey 要获取值的    键
     * @return String
     */
    public static String getPripertyPath(String Profilename,String proPath,String strkey){
        //获取word配置文件
        Properties p=new Properties();
        InputStream is;
        String basePath = "";
        //这样也能取到scr的目录
        //String basePath = request.getSession().getServletContext().getRealPath("/"); +/WEB-INF/classes/
        if(proPath==null || "".equals(proPath)){
            basePath = Thread.currentThread().getContextClassLoader().getResource("/").getPath();
        }else{
            basePath = proPath;
        }
        try {
            //获取陪着文件
            is = new FileInputStream(basePath+"/"+Profilename);
            p.load(is);
            is.close();
        } catch (FileNotFoundException e) {
            System.out.println("未找到"+basePath+"目录下的"+Profilename+"配置文件");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("IO异常");
            e.printStackTrace();
        }
        String value =  p.getProperty(strkey);
        return  value;
    }

    public static void main(String a[]){
        Object ss = BinaryCodec.class.getProtectionDomain().getCodeSource().getLocation();
        System.out.println(ss);


        System.out.println("java_vendor:"  + System.getProperty( "java.vendor" ));
        System.out.println("java_vendor_url:"
                + System.getProperty("java.vendor.url" ));
        System.out.println("java_home:"  + System.getProperty( "java.home" ));
        System.out.println("java_class_version:"
                + System.getProperty("java.class.version" ));
        System.out.println("java_class_path:"
                + System.getProperty("java.class.path" ));
        System.out.println("os_name:"  + System.getProperty( "os.name" ));
        System.out.println("os_arch:"  + System.getProperty( "os.arch" ));
        System.out.println("os_version:"  + System.getProperty( "os.version" ));
        System.out.println("user_name:"  + System.getProperty( "user.name" ));
        System.out.println("user_home:"  + System.getProperty( "user.home" ));
        System.out.println("user_dir:"  + System.getProperty( "user.dir" ));
        System.out.println("java_vm_specification_version:"
                + System.getProperty("java.vm.specification.version" ));
        System.out.println("java_vm_specification_vendor:"
                + System.getProperty("java.vm.specification.vendor" ));
        System.out.println("java_vm_specification_name:"
                + System.getProperty("java.vm.specification.name" ));
        System.out.println("java_vm_version:"
                + System.getProperty("java.vm.version" ));
        System.out.println("java_vm_vendor:"
                + System.getProperty("java.vm.vendor" ));
        System.out
                .println("java_vm_name:"  + System.getProperty( "java.vm.name" ));
        System.out.println("java_ext_dirs:"
                + System.getProperty("java.ext.dirs" ));
        System.out.println("file_separator:"
                + System.getProperty("file.separator" ));
        System.out.println("path_separator:"
                + System.getProperty("path.separator" ));
        System.out.println("line_separator:"
                + System.getProperty("line.separator" ));

    }

}
